<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;

class IntegralGoods extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_v2_integral_shop_goods';




    protected $append = [

        'spe_name'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 18:54
     * @功能说明:规格名字
     */
    public function getSpeNameAttr($value,$data){

        if(!empty($data['spe_id'])){

            $spe_model = new GoodsSpePrice();

            $info = $spe_model->dataInfo(['id'=>$data['spe_id']]);

            if(!empty($info)){

                return $info['spe_name_text'];
            }

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:34
     * @功能说明:积分列表可以通过商品名字查询
     */
    public function dataGoodsList($dis,$page=10){

        $data = $this->alias('a')
            ->join('longbing_card_v2_shop_goods b','a.goods_id = b.id','left')
            ->where($dis)
            ->field('a.*,b.name as goods_name')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;


    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:54
     * @功能说明:初始化活动时间
     */
    public function initAtv(){

        $this->where('start_time','>',time())->update(['atv_status'=>1]);

        $this->where('start_time','<',time())->update(['atv_status'=>2]);

        $this->where('end_time','>',time())->update(['atv_status'=>3]);

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-25 17:58
     * @功能说明:修改
     */
    public function updateAtvStock($integral_id,$goods_id,$spe_id,$num,$type=1){

        $dis = [

            'atv_id'  => $integral_id,

            'goods_id'=> $goods_id,

            'spe_id'  => $spe_id

        ];

        $integral_goods_model = new IntegralGoods();

        $find = $integral_goods_model->dataInfo($dis);

        if($type==1){

            if(empty($find)){

                return ['code'=>500,'msg'=>'积分活动已下架'];

            }

            if($find['have_stock']+$num>$find['stock']){

                return ['code'=>500,'msg'=>'可兑换数量不足'];

            }

            $integral_goods_model->where($dis)->update(['have_stock'=> Db::Raw("have_stock+$num")]);

        }else{

            if(!empty($find)){

                $integral_goods_model->where($dis)->update(['have_stock'=> Db::Raw("have_stock-$num")]);
            }

        }

        return true;

    }






}